<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="公告标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入公告标题"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间段">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- =========================================== -->
    <el-row :gutter="10" style="margin-bottom: 20px">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-permission="['SYSTEM', 'ADMIN']"
          >新增</el-button
        >
      </el-col>
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <!-- 表格数据 -->
    <el-table
      :data="tableData"
      stripe
      v-loading="loading"
      border
      style="margin-bottom: 20px"
      :default-sort="{ prop: 'date', order: 'descending' }"
    >
      <el-table-column
        label="公告标题"
        prop="title"
        align="center"
      ></el-table-column>
      <!-- <el-table-column label="创建人" prop="creator" align="center"></el-table-column> -->
      <el-table-column
        label="创建时间"
        prop="createTime"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        label="更新时间"
        prop="createTime"
        align="center"
        sortable
      ></el-table-column>
      <el-table-column
        label="公告内容"
        prop="information"
        align="center"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="updateNotice(scope.row)"
            v-permission="['SYSTEM', 'ADMIN']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteNotice(scope.row)"
            v-permission="['SYSTEM', 'ADMIN']"
            >删除</el-button
          >
          <el-button
            size="mini"
            @click="viewNotice(scope.row)"
            v-permission="['USER']"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页v-show="total > 0" -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageCurrent"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="total"
    ></el-pagination>
    <!-- 新增和编辑 -->
    <el-dialog :title="dialogTitle" :visible.sync="open" @close="close">
      <el-form
        :model="form"
        label-width="80px"
        :rules="formRules"
        ref="formRef"
      >
        <el-form-item label="公告标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <!-- <el-form-item v-if="editId == ''" label="创建人" prop="creator">
                    <el-input v-model="form.creator"></el-input>
                </el-form-item> -->
        <el-form-item label="公告内容" prop="information">
          <el-input type="textarea" v-model="form.information"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="resetForm">重置</el-button>
      </div>
    </el-dialog>
    <!-- 查看详情 -->
    <el-dialog :title="dialogTitle" :visible.sync="viewOpen" @close="close">
      <el-form
        :model="form"
        label-width="80px"
        :rules="formRules"
        ref="formRef"
      >
        <el-form-item label="公告标题" prop="title">
          {{ form.title }}
        </el-form-item>
        <el-form-item label="公告内容" prop="information">
          {{ form.information }}
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  selectNotice,
  saveNotice,
  updateNotice,
  deleteNotice,
} from "@/api/notice";
export default {
  data() {
    return {
      // 表格数据
      tableData: [],
      // 表格加载
      loading: false,
      // 表格条数
      total: undefined,
      // 查询参数
      queryParams: {
        title: undefined,
        pageCurrent: 1,
        pageSize: 10,
      },
      // 弹出框标题
      dialogTitle: "",
      // 弹出框开关
      open: false,
      form: {
        title: undefined,
        creator: undefined,
        information: undefined,
      },
      // 表单校验规则
      formRules: {
        title: [{ required: true, message: "请输入公告标题", tagger: "blur" }],
        creator: [{ required: true, message: "请输入创建人", tagger: "blur" }],
        information: [
          { required: true, message: "请输入公告内容", tagger: "blur" },
        ],
      },
      editId: "",
      username: "",
      dateRange: [],
      viewOpen: false,
    };
  },
  methods: {
    // 分页==========================================================
    handleSizeChange(pageSizeVal) {
      this.queryParams.pageSize = pageSizeVal;
      this.getList();
    },
    handleCurrentChange(pageCurrentVal) {
      this.queryParams.pageCurrent = pageCurrentVal;
      this.getList();
    },
    async getList() {
      this.loading = true;
      let res = await selectNotice({
        ...this.queryParams,
        dateRange: this.dateRange,
      });
      console.log(res);
      if (res.code == 200) {
        this.tableData = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      }
    },
    // 查看
    viewNotice(row) {
      this.dialogTitle = "查看";
      this.viewOpen = true;
      this.form = JSON.parse(JSON.stringify(row));
    },
    // 添加
    handleAdd() {
      this.dialogTitle = "添加";
      this.open = true;
      this.editId = "";
      this.form = {
        title: undefined,
        creator: undefined,
        information: undefined,
      };
    },
    // 编辑
    updateNotice(row) {
      this.dialogTitle = "编辑";
      this.open = true;
      this.editId = row.id;
      this.form = JSON.parse(JSON.stringify(row));
    },
    // 提交表单
    async submitForm() {
      if (this.editId != "") {
        // 编辑
        let res = await updateNotice({
          title: this.form.title,
          id: this.editId,
          information: this.form.information,
        });
        if (res.code == 200) {
          this.open = false;
          this.$message.success(res.msg);
          this.getList();
        }
      } else {
        // 添加
        this.$refs.formRef.validate(async (valid) => {
          if (valid) {
            let res = await saveNotice(this.form);
            if (res.code == 200) {
              this.open = false;
              this.$message.success(res.msg);
              this.getList();
            }
          }
        });
      }
    },
    // 重置表单
    resetForm() {
      this.$refs.formRef.resetFields();
    },
    // 删除
    async deleteNotice(row) {
      const confirm = await this.$confirm(
        "此操作将永久删除, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirm !== "confirm") {
        return this.$message.info("已取消删除");
      }
      // 确定删除
      deleteNotice({ id: row.id }).then((res) => {
        if (res.code !== 200) {
          this.$message.error("删除失败");
        } else {
          this.$message.success("删除成功");
          this.getList();
        }
      });
    },
    close() {
      this.$refs.formRef.resetFields();
      this.form = {
        title: undefined,
        creator: undefined,
        information: undefined,
      };
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageCurrent = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.dateRange = [];
      this.$refs.queryForm.resetFields();
      this.getList();
    },
  },
  mounted() {
    this.getList();
    let roles = JSON.parse(sessionStorage.getItem("roles"));
    this.username = roles.name;
  },
};
</script>
